package com.albaba.designpattern.iterator.text_001;

import java.util.ArrayList;

/**
 * @Description: TODO
 * @Author zx
 * @Date 2025/2/25 16:32
 * @Version 1.0
 */
public class ConcreteIterator<E> implements Iterator<E> {

    // 游标
    private int cursor;

    // 容器
    private ArrayList<E> arrayList;

    public ConcreteIterator(ArrayList<E> arrayList) {
        this.cursor = 0;
        this.arrayList = arrayList;
    }

    @Override
    public boolean hasNext() {
        return cursor != arrayList.size();
    }

    @Override
    public void next() {
        cursor++;
    }

    @Override
    public E currentItem() {
        if (cursor >= arrayList.size()) {
            throw new RuntimeException("游标越界");
        }

        return arrayList.get(cursor);
    }
}
